package view;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import utils.DataInitializer;
import utils.TextPrinter;
import McdaMethods.ElectreI;
import McdaMethods.ElectreIv;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.JTextField;

public class MainWindow {

	JButton btnElectreI ;
	JButton btnElectreIv ;
	
  private JFrame frame;
  JTextArea textArea;
  TextPrinter textPrinter;

  DataInitializer initializer;

  ElectreI electreI;
  ElectreIv electreIv;
  private JTextField txtCtresholdsminicsv;
  private JTextField txtCwagiminicsv;
  private JTextField txtCdaneminicsv;


  /**
   * Launch the application.
   */
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          MainWindow window = new MainWindow();
          window.frame.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

  /**
   * Create the application.
   */
  public MainWindow() {
    initialize();
    textPrinter = TextPrinter.getInstance();
    textPrinter.setTargetComponent(textArea);
  }

  /**
   * Initialize the contents of the frame.
   */
  private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 700, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(
        new FormLayout(new ColumnSpec[] {
    		FormFactory.RELATED_GAP_COLSPEC,
    		FormFactory.DEFAULT_COLSPEC,
    		FormFactory.RELATED_GAP_COLSPEC,
    		ColumnSpec.decode("default:grow"),
    		FormFactory.RELATED_GAP_COLSPEC,
    		FormFactory.DEFAULT_COLSPEC,
    		FormFactory.RELATED_GAP_COLSPEC,
    		FormFactory.DEFAULT_COLSPEC,
    		FormFactory.RELATED_GAP_COLSPEC,
    		FormFactory.DEFAULT_COLSPEC,},
    	new RowSpec[] {
    		FormFactory.RELATED_GAP_ROWSPEC,
    		FormFactory.DEFAULT_ROWSPEC,
    		FormFactory.RELATED_GAP_ROWSPEC,
    		FormFactory.DEFAULT_ROWSPEC,
    		FormFactory.RELATED_GAP_ROWSPEC,
    		FormFactory.DEFAULT_ROWSPEC,
    		FormFactory.RELATED_GAP_ROWSPEC,
    		FormFactory.DEFAULT_ROWSPEC,
    		FormFactory.RELATED_GAP_ROWSPEC,
    		RowSpec.decode("default:grow"),}));

    JButton btnLoaddata = new JButton("loadData");
    btnLoaddata.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
    	  btnElectreI.setEnabled(true);
    	  btnElectreIv.setEnabled(true);
        initializer = new DataInitializer(txtCwagiminicsv.getText(), txtCdaneminicsv.getText(), txtCtresholdsminicsv.getText());
      }
    });
    
    JLabel lblNewLabel = new JLabel("Data file path:");
    frame.getContentPane().add(lblNewLabel, "2, 4, right, default");
    
    txtCdaneminicsv = new JTextField();
    txtCdaneminicsv.setText("C://dane_mini.csv");
    frame.getContentPane().add(txtCdaneminicsv, "4, 4, fill, default");
    txtCdaneminicsv.setColumns(10);
    
    JLabel lblNewLabel_1 = new JLabel("Weights file path");
    frame.getContentPane().add(lblNewLabel_1, "2, 6, right, default");
    
    txtCwagiminicsv = new JTextField();
    txtCwagiminicsv.setText("C://wagi_mini.csv");
    frame.getContentPane().add(txtCwagiminicsv, "4, 6, fill, default");
    txtCwagiminicsv.setColumns(10);
    
    JLabel lblTresholdsFilePath = new JLabel("Tresholds file path");
    frame.getContentPane().add(lblTresholdsFilePath, "2, 8, right, default");
    
    txtCtresholdsminicsv = new JTextField();
    txtCtresholdsminicsv.setText("C://tresholds_mini.csv");
    frame.getContentPane().add(txtCtresholdsminicsv, "4, 8, fill, default");
    txtCtresholdsminicsv.setColumns(10);
    frame.getContentPane().add(btnLoaddata, "6, 8");

     btnElectreI = new JButton("do ELECTRE I");
     btnElectreI.setEnabled(false);
    btnElectreI.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        electreI = new ElectreI();
        electreI.setInputData(initializer.getAlternativesNames(), initializer.getAlternativesArray(), initializer.getCriterionNames(), initializer.getWeightValues());
        electreI.work();
      }  
    });
    frame.getContentPane().add(btnElectreI, "8, 8");
    
     btnElectreIv = new JButton("do ELECTRE Iv");
     btnElectreIv.setEnabled(false);
    btnElectreIv.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        electreIv = new ElectreIv();
        electreIv.setInputData(initializer.getAlternativesNames(), initializer.getAlternativesArray(), initializer.getCriterionNames(), initializer.getWeightValues(), initializer.getTresholdValues());
        electreIv.work();
      }  
    });
    frame.getContentPane().add(btnElectreIv, "10, 8");

    JScrollPane scrollPane = new JScrollPane();
    frame.getContentPane().add(scrollPane, "2, 10, 9, 1, fill, fill");

    textArea = new JTextArea();
    scrollPane.setViewportView(textArea);
    textArea.setColumns(2);
  }

}
